import { fileURLToPath, URL } from 'node:url'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { VarletUIResolver } from 'unplugin-vue-components/resolvers'
import viteCompression from 'vite-plugin-compression'
import { ViteImageOptimizer } from 'vite-plugin-image-optimizer'
import { visualizer } from 'rollup-plugin-visualizer'
export const prodConfig = {
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  plugins: [
    vue(),
    Components({
      resolvers: [VarletUIResolver()]
    }),
    AutoImport({
      imports: [
        'vue',
        'pinia',
        'vue-router',
        {
          nprogress: ['start', 'done', 'isStarted']
        }
      ],
      dts: true,
      dirs: ['./src/pinia/**', './src/components/**/*', './src/hooks/*.ts', './src/utils/*.ts'],
      resolvers: [VarletUIResolver({ autoImport: true })]
    }),
    // gzip 压缩
    viteCompression(),
    // 图片压缩
    ViteImageOptimizer(),
    visualizer()
  ],
  build: {
    rollupOptions: {
      output: {
        manualChunks: (id: any) => {
          // 将 node_modules 中的代码单独打包成一个 JS 文件
          if (id.includes('node_modules')) {
            return 'vendor'
          }
        }
      }
    }
  }
}
export default prodConfig
